package com.wenlin.myproduction.bean;

import android.os.Build;
import android.text.TextUtils;

import com.wenlin.myproduction.MyApplication;
import com.wenlin.myproduction.util.DeviceUtil;
import com.wenlin.myproduction.util.MD5Util;
import com.wenlin.myproduction.util.SharePreferenceUtil;
import com.wenlin.myproduction.util.UserManage;

import java.io.Serializable;
import java.util.HashMap;

public class RequestBean implements Serializable {
    private static String mToken;
    private static String mVersionName;
    private static String mVersionCode;

    public static HashMap<String, Object> pastMap(String api) {
        api = api.toLowerCase().trim();
        HashMap<String, Object> map = new HashMap<String, Object>();
        String sn = getSn() ;//sn取uid
        String timestamp = System.currentTimeMillis() / 1000 + "";
        String md5 = sn + "-" + timestamp + "-" + api;
        String authkey = MD5Util.md5(md5);
        map.put("equipType", Build.MODEL);//设备类型
        map.put("sn", sn);
        map.put("timestamp", timestamp);
        map.put("authkey", authkey);
        map.put("versionName", getVersionName());
        map.put("versionCode", getVersionCode());
        String token = getToken();
        if (!TextUtils.isEmpty(token)) {
            map.put("token", token);
        }
//        LogUtils.e("http://test.app.api.myenglish.com.cn:8089"+api+"?sn="+sn+"&timestamp="+timestamp+"&authkey="+authkey+"&token="+token);
//        LogUtils.e("sn="+sn+"\ntimestamp="+timestamp+"\nauthkey="+authkey+"\ntoken="+token);
        return map;

    }

    public static String getSn() {
        String sn = ""+ UserManage.getInstance().getUid() ;//sn取uid
        return sn;
    }

    public static String getToken() {
        if(TextUtils.isEmpty(mToken)){
            return SharePreferenceUtil.getInstance().getString("token");
        }
        return mToken;
    }

    public static void setToken(String token) {
        mToken = token;
        SharePreferenceUtil.getInstance().putString("token", token);
    }

    public static String getVersionName() {
        if(TextUtils.isEmpty(mVersionName)){
            mVersionName = DeviceUtil.getAppVersionName(MyApplication.getMyApplicationContext());
        }
        return mVersionName;
    }

    public static String getVersionCode() {
        if(TextUtils.isEmpty(mVersionCode)){
            mVersionCode = ""+DeviceUtil.getAppVersionNo(MyApplication.getMyApplicationContext());
        }
        return mVersionCode;
    }
}
